<!DOCTYPE html>
<html>
<head>
    <title>购物车</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" charset="UTF-8">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/bootstrap-table.min.css" rel="stylesheet">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/bootstrap-table.min.js"></script>
    <script type="text/javascript" src="js/bootstrap-table-zh-CN.min.js"></script>
</head>
<body>
<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title text-center">购物车</h3>
    </div>
    <div class="panel-body">

        <div id="toolbar" class="btn-group">
            <button id="btn_save" type="button" class="btn btn-primary">
                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>提交订单
            </button>
            <button id="btn_delete" type="button" class="btn btn-primary">
                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除产品
            </button>
            <button id="btn_product" type="button" class="btn btn-primary">
                <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>继续购买
            </button>
        </div>

        <table data-toggle="table" id="table" data-height="400"
               data-classes="table table-hover" data-striped="true"
               data-pagination="false" data-side-pagination="server"
               data-show-refresh="true" data-show-toggle="true"
               data-show-columns="true" data-toolbar="#toolbar">
            <thead>
            <tr>
                <th data-field="state" data-checkbox='ture'></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
            </tr>
            </thead>
        </table>
    </div>
</div>
</body>
<script type="text/javascript">
    $(document).ready(function() {
        $("button[name='toggle']").height(20);
        $("button[name='refresh']").height(20);
    });

    //删除按钮点击事件（可以批量删除）
    $("#btn_delete").click(function() {
        var list = $("table").bootstrapTable('getSelections');
        if (list == null || list.length <= 0) {
            alert("未选中任何项！");
            return;
        }

        var idList = new Array();
        for (var i = 0; i < list.length; i++) {
            idList[i] = list[i].productId;
        }
        if (confirm("是否删除选中的数据?")) {
            $.ajax({
                url : "shopcart/deleteByList",
                type : 'post',
                dataType : 'json',
                data : {
                    idList : idList
                },
                success : function(data) {
                    alert("删除成功！");
                    //重新加载表格
                    $("#table").bootstrapTable("refresh");
                }
            });
        }
    });

    $("#btn_save").click(function() {
        if (!confirm("是否确定生成订单?"))
            return;

        $.ajax({
            url : "shopcart/order",
            method : 'post',
            dataType:'json',
            success : function(data) {
                if (data.status == "success") {
                    alert("生成订单成功！");
                    window.location.href="order.html";
                }
                else if (data.status == "notlogin") {
                    alert("尚未登录，点确定按钮进入登录页面!");
                    window.location.href="login.html";
                }
                else {
                    alert("生成订单失败，" + data.message);
                    $("#table").bootstrapTable("refresh");
                }

            }
        });
    });


    $("#btn_product").click(function() {
        window.location.href="product.html"
    });

    $("#table").bootstrapTable({
        url : "shopcart/list",
        clickToSelect : true,
        dataType : "json",
        contentType : "application/x-www-form-urlencoded;charset=utf-8",
        method : 'get',
        responseHandler : function(res) {
            return res;
        },
        columns : [
        {
            field : 'state',
        },
        {
            field : 'productId',
            title : 'ID',
            align : 'center'
        },
        {
            field : 'productName',
            title : '产品名称',
            align : 'center'
        },
        {
            field : 'price',
            title : '价格',
            align : 'center'
        },
        {
            field : 'count',
            title : '购买数量',
            align : 'center'
        }]
    });
</script>
</html>
